package com.uuah.server.lifecycle;

import java.lang.reflect.Constructor;

import com.uuah.config.i18n.CoreMessages;
import com.uuah.exception.UuahRuntimeException;
import com.uuah.server.api.IUuahContext;
import com.uuah.server.config.i18n.ServerMessages;
import com.uuah.server.context.notification.ServerNotification;
import com.uuah.server.context.notification.UuahContextNotification;
import com.uuah.utils.ClassUtils;

/**
 *
 * TODO comment ...
 *
 * <p>
 * update record:updator，update time，update content and version
 * </p>
 *
 * @author <a href="jonny_quan@hotmail.com">jonny</a>
 * @date 2009 2009-3-27 下午01:53:08
 * @version 1.0.0
 */
public class NotificationLifecycleObject extends LifecycleObject {
	private String preNotificationName;
	private String postNotificationName;
	private Constructor ctor;

	public NotificationLifecycleObject(Class type) {
		super(type);
	}

	public NotificationLifecycleObject(Class type, Class notificationClass) {
		super(type);

		if (notificationClass == null) {
			throw new IllegalArgumentException(CoreMessages.objectIsNull(
					"notificationClass").toString());
		}

		// make sure the notifiactionClass is properly loaded and
		// initialized
		notificationClass = ClassUtils.initializeClass(notificationClass);

		if (!ServerNotification.class.isAssignableFrom(notificationClass)) {
			throw new IllegalArgumentException(
					"Notification class must be of type: "
							+ ServerNotification.class.getName());
		}

		ctor = ClassUtils.getConstructor(notificationClass, new Class[] {
				Object.class, String.class });
		if (ctor == null) {
			throw new IllegalArgumentException(
					"No constructor defined in Notification class: "
							+ notificationClass
							+ " with arguments (Object.class, String.class)");
		}
	}

	public NotificationLifecycleObject(Class type, Class notificationClass,
			int preNotification, int postNotification) {
		this(type, notificationClass);
		setPreNotificationName(UuahContextNotification
				.getActionName(preNotification));
		setPostNotificationName(UuahContextNotification
				.getActionName(postNotification));
	}

	public String getPostNotificationName() {
		return postNotificationName;
	}

	public void setPostNotificationName(String postNotificationName) {
		this.postNotificationName = postNotificationName;
	}

	public String getPreNotificationName() {
		return preNotificationName;
	}

	public void setPreNotificationName(String preNotificationName) {
		this.preNotificationName = preNotificationName;
	}

	// @Override
	public void firePreNotification(IUuahContext context) {
		if (getPreNotificationName() != null) {
			setPreNotification(createNotification(context,
					getPreNotificationName()));
		}
		super.firePreNotification(context);

	}

	// @Override
	public void firePostNotification(IUuahContext context) {
		if (getPostNotificationName() != null) {
			setPostNotification(createNotification(context,
					getPostNotificationName()));
		}
		super.firePostNotification(context);
	}

	protected ServerNotification createNotification(IUuahContext context,
			String action) {
		try {
			return (ServerNotification) ctor.newInstance(new Object[] {
					context, action });
		} catch (Exception e) {
			throw new UuahRuntimeException(ServerMessages
					.failedToCreate("Notification:" + action), e);
		}
	}
}